package com.chx.controller;

import com.chx.entity.User;
import com.chx.service.UserService;
import com.chx.utils.VerifyCodeUtils;
import com.chx.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/loginView")
    public String loginView(){
        return "login";
    }

    @RequestMapping("/verifyCode")
    public void verifyCode(HttpSession httpSession, HttpServletResponse response){
        userService.getVerifyCode(httpSession,response);
    }

    @RequestMapping("/registerView")
    public String registerview(){
        return "register";
    }

    @RequestMapping("/login")
    public String login(HttpSession httpSession,UserVo vo){
        try {
            userService.login(httpSession, vo);
        }catch (Exception e){
            return "login";
        }
        return "redirect:../index";
    }

    @RequestMapping("/register")
    public String register(UserVo vo){
        userService.register(vo);
        return "redirect:loginView";
    }

    /**
     * 退出登录
     */
    @RequestMapping("logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();//退出用户
        return "redirect:loginView";
    }
}
